package org.bank;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.bank.dto.AccountTO;

/**
 * This class works as both Business delegate and service locator.
 * These concerns should also be separated in different classes.
 *
 */
//The delegate makes it possible to translate the messages sent from the bussines logic, into user understandable messages.
public class BankDelegate {
	
	private Bank bank;
	private static String jndiName = "BankEAR/BankBean/local";
	
	public BankDelegate(){
	    Context context = null;
        try {
            context = new InitialContext();
            bank = (Bank) context.lookup(jndiName);
        } catch (NamingException ex) {
            throw new IllegalStateException("Cannot find " + jndiName + ", " + ex, ex.getCause());
        } finally {
            try {
                context.close();
            } catch (NamingException ex) {
                throw new IllegalStateException("Cannot close InitialContext: " + ex, ex.getCause());
            }
        }		
	}
	
	//calling the create account from the bank system, and translating the result
	public String createAccount(AccountTO accountTO){
		try {
			boolean result = bank.createAccount(accountTO);
			if(result == true)
				return "The account was successfully created with number " + accountTO.ID;
			else
				return "Account already exists";
		} catch (Exception e) {
			e.printStackTrace();
			return "Exception: " + e.getMessage();
		}
	}
	
	//calling the edit account from the bank system and translating the result
	public String editAccount(int accountNumber, String firstName, String lastName, String address, String phoneNumber)
	{
		try
		{
			boolean result = bank.editAccount(accountNumber, firstName, lastName, address, phoneNumber);
			if(result == true)
				return "The account was successfully edited";
			else
				return "Account could not be found";
		} catch (Exception e) {
			e.printStackTrace();
			return "Exception: " + e.getMessage();
		}
	}
	
	
	public AccountTO getAccount(int accountNumber)
	{
		try
		{
			return bank.getAccount(accountNumber);
		} catch (Exception e) {
			return null; //meaning sth went wrong
		}
	}
	
	public String transferMoney(int accountNumber1, int accountNumber2, double amount)
	{
		try
		{
			int result = bank.transferMoney(accountNumber1, accountNumber2, amount);
			switch(result)
			{
			case 0: 
				return "The amount was transfered.";
			case 1:
				return "The account with number " + accountNumber1  +" does not exist.";
			case 2:
				return "The second account with number " + accountNumber2  +" does not exist.";
			case 3:
				return "The specified amount is not available in the account with number: " + accountNumber1;
			default:
					return "";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "Exception: " + e.getStackTrace();
		}
	}

}
